#!/bin/bash

mkdir build
mkdir ~/.vim

neovim_retry=20
#top dir

development_kits="gdb"

yum -v &> /dev/zero
os=$?

echo -n "Whether to install common development kits? (yes/no)"
read enter

#echo -n "only vim ? (yes/no)"
#read only_vim

if [[ "$os" != 127 ]]
then
	echo "centos"
	if [[ $enter = "yes" || $enter = "y" ]]
	then
		echo "install development kits"
		sudo dnf install $development_kits -y
	fi
	sudo dnf install curl ncurses-devel libX11-devel libXpm-devel perl perl-devel git cmake zsh -y
	sudo dnf install -y gcc gcc-c++ texinfo
	sudo dnf install powerline-fonts -y
	sudo dnf builddep vim-X11 -y
	sudo dnf install xclip -y
	sudo dnf install python3-devel -y
else
	echo "ubuntu"
	#enable src
	sudo sed -i "s/# deb-src/deb-src/g" /etc/apt/sources.list
	if [[ $enter = "yes" || $enter = "y" ]]
	then
		echo "install development kits"
		sudo apt install $development_kits -y
	fi
	sudo apt update
	# sudo apt install nodejs -y
	sudo apt install npm  cmake -y
	sudo apt install curl gettext -y
	sudo apt install fonts-powerline
	sudo apt install libncurses5-dev -y
	sudo apt install libcairo2-dev libx11-dev libxpm-dev libxt-dev -y
	sudo apt install python3-dev ruby-dev lua5.2 liblua5.2-dev libperl-dev git cmake zsh -y
    sudo apt install python3-pip
	sudo apt build-dep vim-gtk -y
	sudo apt install clangd -y
	sudo apt install xclip -y

fi

echo "install node.js"
sudo ./node-install.sh

git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim --depth 1

export GIT_SSL_NO_VERIFY=1
git clone https://git.a-boat.cn:2021/yarra/vim-plugins.git --depth 1

echo -n "use vim or neovim? 1:vim 2:neovim"
echo -n " "
read enter
if [ $enter = "1" ]
then
	sudo apt build-dep vim -y
    tar -vxf ./vim-plugins/vim.tar.bz2  -C ./build

    cd ./build/vim

    make distclean
    CFLAGS=-fPIC ./configure --with-features=huge \
        --enable-multibyte \
        --enable-rubyinterp=yes \
        --enable-python3interp=yes \
        --with-python3-config-dir=$(python3-config --configdir) \
        --enable-perlinterp=yes \
        --enable-luainterp=yes \
        --enable-cscope  \
        --with-tlib=ncurses

    make -j4 VIMRUNTIMEDIR=/usr/local/share/vim/vim82

    sudo make install
    sudo update-alternatives --install /usr/bin/editor editor /usr/local/bin/vim 1
    sudo update-alternatives --set editor /usr/local/bin/vim
    sudo update-alternatives --install /usr/bin/vi vi /usr/local/bin/vim 1
    sudo update-alternatives --set vi /usr/local/bin/vim
else
    sudo apt install libtool-bin
    python3 -m pip install pynvim
    sudo npm install -g neovim
    if [ ! -e "~/.config" ]
    then
        mkdir ~/.config
    fi
    tar -vxf ./vim/nvim.tar.bz2 -C ~/.config/
    tar -vxf ./vim-plugins/neovim.tar.bz2 -C ./build
    cd ./build/neovim
    make CMAKE_BUILD_TYPE=RelWithDebInfo CMAKE_INSTALL_PREFIX=/usr
    make_ret=$?
    while [[ $neovim_retry -gt 1 && $make_ret != 0 ]]
    do
        echo "try again: ""$neovim_retry"
        neovim_retry=`expr $neovim_retry - 1`
        make CMAKE_BUILD_TYPE=RelWithDebInfo CMAKE_INSTALL_PREFIX=/usr
        make_ret=$?
    done
    sudo make install
    sudo ln -s /usr/bin/nvim /usr/bin/vim
fi

cd ../../

cp ./vim/vimrc ~/.vimrc
cp ./vim/coc-settings.json ~/.vim/

tar -vxf ./vim-plugins/vim-plugin.tar.bz2 -C ~/.vim/

tar -vxf ./vim/colors.tar -C ~/.vim/

echo "install vim success!"
echo "luanch vim exec PluginInstall"

######vim is ok########
cd ./shell

./ohMyZsh_install.sh

chown ${USER}:${USER} ~/.oh-my-zsh

#####zsh is ok#########

